/**
 * 
 */
package edu.umd.clip.lm.model;

import java.nio.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.umd.clip.lm.util.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public abstract class AbstractClusterStorage extends XMLizable {
	public static final String XML_ELEMENT_NAME = "cluster-storage";
	public static final String XML_ATTR_TYPE_NAME = "type";
	
	public static final int FILE_STORAGE_TYPE = 1;
	
	public abstract ByteBuffer loadCluster(int clusterid);
	public abstract void addCluster(int clusterid, ByteBuffer data);
	public abstract void openForReading();
	public abstract void openForWriting(int maxClusters);
	public abstract void close();
	public abstract boolean isOpen();
	public abstract int getMaxClusterId();
	
	public static AbstractClusterStorage fromXML(Element xmlnode) {
		String type = xmlnode.getAttribute(XML_ATTR_TYPE_NAME);
		switch (Integer.parseInt(type)) {
		case FILE_STORAGE_TYPE:
			return new FileClusterStorage(xmlnode);
		default:
			return null;
		}
	}
	@Override
	public Element createXML(Document doc) {
		Element element = doc.createElement(XML_ELEMENT_NAME);
		element.setAttribute(XML_ATTR_TYPE_NAME, Integer.toString(getType()));
		return element;
	}

	@Override
	public String getElementName() {
		return XML_ELEMENT_NAME;
	}
	
	public abstract int getType();
}
